package co.com.javeriana.aes.oms.ejb.user.service;

import co.com.javeriana.aes.oms.common.entity.Customer;
import co.com.javeriana.aes.oms.ejb.user.dto.User;
import co.com.javeriana.aes.oms.ejb.user.dto.UserType;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@WebService(serviceName = "Client")
@Stateless()
public class UserWebService {

    @EJB
    private UserServiceBeanLocal serviceBeanLocal;

    @WebMethod(operationName = "createClient")
    public Integer createClient(@WebParam (name = "user")  User user){
        Integer s = null;
        try {
            s = serviceBeanLocal.createCustomer(user);            
        } catch (Exception ex) {            
            Logger.getLogger(UserWebService.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return s;
    }
    
    
    /**
     *
     * @param clientID
     * @return
     */
    @WebMethod(operationName = "findClientById")
    public User findClientById(@WebParam(name = "clientId") int clientID) {

        Customer customer = null;
        User user = null;
        
        try {
            customer = serviceBeanLocal.getCustomer(clientID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (customer!=null){
            
            user = new User();
            user.setCreditCardNumber(customer.getCreditcardnumber() != null ? customer.getCreditcardnumber() : null);
            user.setCreditCardType(customer.getCreditcardtype() != null ? customer.getCreditcardtype():null);
            user.setCustomerId(customer.getCusid());
            user.setEmail(customer.getEmail());
            user.setFirstName(customer.getFname());
            user.setLastName(customer.getLname());
            user.setPassword(customer.getPassword());
            user.setPhoneNumber(customer.getPhonenumber());
            user.setStatus(customer.getStatus());
            user.setType(UserType.valueOf(customer.getType()) );
        }
        return user;
    }    
    
    /**
     * 
     * @param email
     * @return 
     */
    @WebMethod(operationName = "findClientByEmail")
    public User findClientByEmail(@WebParam(name = "email") String email) {

        Customer customer = null;
        User user = null;
        
        try {
            customer = serviceBeanLocal.getCustomerByEmail(email);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (customer!=null){
            
            user = new User();
            user.setCreditCardNumber(customer.getCreditcardnumber());
            user.setCreditCardType(customer.getCreditcardtype());
            user.setCustomerId(customer.getCusid());
            user.setEmail(customer.getEmail());
            user.setFirstName(customer.getFname());
            user.setLastName(customer.getLname());
            user.setPassword(customer.getPassword());
            user.setPhoneNumber(customer.getPhonenumber());
            user.setStatus(customer.getStatus());
            user.setType(UserType.valueOf(customer.getType()) );
        }
        return user;
    }
    

    /**
     * 
     * @param email
     * @param password
     * @return 
     */
    @WebMethod(operationName = "validateUser")
    public boolean validateUser(@WebParam(name = "email") String email, @WebParam(name = "password") String password) {
        boolean val = false;
        try {
            val = serviceBeanLocal.validateUser(email, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return val;
    }
}
